package com.dwxt.pay.service;

import com.dwxt.common.base.BaseResult;
import com.dwxt.common.base.StatusCode;
import com.dwxt.common.util.JsonUtils;
import com.dwxt.common.wxpay.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.SortedMap;

@Service
@Slf4j
public class WXService {

    public BaseResult pay(Map<String, String> params) throws Exception {
        //PayAccount account = payAccountService.selectEffectivePayAccount(PurchaseMethod.WX);
        Integer type = Integer.parseInt(params.get("type"));
        WXPayConfig wxPayConfig;
        if (type == 3) {
            wxPayConfig = new MinWXPayConfig();
        } else {
            wxPayConfig = new MyWXPayConfig();
        }
        //构造参数
        MyWXReqUtil myWxReqUtil = new MyWXReqUtil(wxPayConfig);
        SortedMap<String, String> data = myWxReqUtil.buildPayParams(params);
        /** wxPay.unifiedOrder 这个方法中调用微信统一下单接口 */
        WXPay wxPay = new WXPay(wxPayConfig);
        Map<String, String> respData = wxPay.unifiedOrder(data);
        if (respData.get("return_code").equals("SUCCESS") && respData.get("result_code").equals("SUCCESS")) {
            //构造返回结果
            Map<String, String> repData = myWxReqUtil.buildResult(respData);
            return BaseResult.ok(repData);
        } else {
            log.error("统一下单失败：" + JsonUtils.objectToJson(respData));
            throw new RuntimeException();
        }
    }


    public BaseResult queryPayResult(String orderId) throws Exception {
        //PayAccount account = payAccountService.selectEffectivePayAccount(PurchaseMethod.WX);
        MyWXPayConfig myWXPayConfig = new MyWXPayConfig();
        //构造查询参数
        MyWXReqUtil myWxReqUtil = new MyWXReqUtil(myWXPayConfig);
        SortedMap<String, String> data = myWxReqUtil.buildQueryParams(orderId);

        WXPay wxPay = new WXPay(myWXPayConfig);
        Map<String, String> respData = wxPay.orderQuery(data);
        if ("SUCCESS".equalsIgnoreCase(respData.get("return_code")) && "SUCCESS".equalsIgnoreCase(respData.get
                ("result_code")) && "SUCCESS".equalsIgnoreCase(respData.get("trade_state"))) {
            //PaymentOrder order = paymentOrderMapper.selectByPrimaryKey(out_trade_no);
            return BaseResult.ok("SUCCESS");
        } else {
            return BaseResult.build(StatusCode.ORDER_PURCHASE_FAIL);
        }
    }


    public BaseResult refund(String orderId, Double price) throws Exception {
        //PayAccount account = payAccountService.selectEffectivePayAccount(PurchaseMethod.WX);
        MyWXPayConfig myWXPayConfig = new MyWXPayConfig();

        MyWXReqUtil myWxReqUtil = new MyWXReqUtil(myWXPayConfig);
        SortedMap<String, String> data = myWxReqUtil.buildRefundParams(orderId, price);

        /** wxPay.unifiedOrder 这个方法中调用微信统一下单接口 */
        WXPay wxPay = new WXPay(myWXPayConfig);
        Map<String, String> respData = wxPay.refund(data);
        if (respData.get("return_code").equals("SUCCESS") && respData.get("result_code").equals("SUCCESS")) {
            return BaseResult.ok("SUCCESS");
        } else {
            log.error("退款失败" + respData.get("return_msg"));
            return BaseResult.ok("FAIL");
        }
    }


}
